package top.buukle.buukle.buuk;

import java.util.Iterator;
import java.util.LinkedList;

public class UpDownIterator implements Iterator {

    LinkedList<Integer> queue = new LinkedList<>();

    public UpDownIterator(int[]  ... arrs) {

        // 计算最大的数组长度
        int maxLength = 0;
        for (int j = 0; j< arrs.length; j++) {
            maxLength = Math.max(arrs[j].length,maxLength);
        }
        // 使用最大的数组长度作为边界,竖着开始遍历二维数组
        for (int i = 0; i < maxLength; i++) {
            for (int j = 0; j< arrs.length; j++) {
                // i 在小于 一维数组的长度情况下,将该坐标的值放进队列中
                if( i < arrs[j].length){
                    queue.offer(arrs[j][i]);
                }
            }
        }
    }

    public static void main(String[] args) {

        int[] v1 = { 1,2};
        int[] v2 = { 3,4,0};
        int[] v3 = { 5,6};

        UpDownIterator upDownIterator = new UpDownIterator(v1,v2,v3);
        while(upDownIterator.hasNext()){
            System.out.println(upDownIterator.next());
        }
    }

    @Override
    public boolean hasNext() {
        return !queue.isEmpty();
    }

    @Override
    public Object next() {
        return queue.pop();
    }
}
